// 声音图表模块
const SoundChart = {
  chartInstance: null,
  
  // 初始化声音图表
  init: function(containerId) {
    const container = document.getElementById(containerId);
    if (!container) {
      console.error(`声音图表容器 ${containerId} 未找到`);
      return null;
    }

    this.chartInstance = echarts.init(container);
    
    // 注册到联动管理器
    ChartSync.registerChart(this.chartInstance);
    
    console.log('声音图表初始化完成');
    return this.chartInstance;
  },

  // 更新声音图表数据
  update: function(timeData, soundData) {
    if (!this.chartInstance) {
      console.error('声音图表未初始化');
      return;
    }

    const option = {
      ...ChartConfig.commonChartConfig,
      title: {
        text: '声音传感器数据',
        left: 'center'
      },
      tooltip: {
        ...ChartConfig.commonTooltipConfig,
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          animation: false,
          lineStyle: {
            color: 'rgba(30, 144, 255, 0.8)',
            width: 1.5
          },
          crossStyle: {
            color: 'rgba(30, 144, 255, 0.8)',
            width: 1.5
          }
        }
      },
      grid: ChartConfig.getGridConfig('small'),
      xAxis: ChartConfig.getXAxisConfig(timeData, 8),
      yAxis: {
        type: 'value',
        name: '声音强度',
        splitLine: {
          show: false
        }
      },
      series: [
        {
          name: '声音传感器S1',
          type: 'line',
          data: soundData.s1,
          symbol: 'none',
          lineStyle: {
            width: 1.5,
            color: '#9370db'
          },
          emphasis: {
            focus: 'none'
          },
          sampling: 'average',
          large: true
        },
        {
          name: '声音传感器S2',
          type: 'line',
          data: soundData.s2,
          symbol: 'none',
          lineStyle: {
            width: 1.5,
            color: '#ba55d3'
          },
          emphasis: {
            focus: 'none'
          },
          sampling: 'average',
          large: true
        },
        {
          name: '声音传感器S3',
          type: 'line',
          data: soundData.s3,
          symbol: 'none',
          lineStyle: {
            width: 1.5,
            color: '#8a2be2'
          },
          emphasis: {
            focus: 'none'
          },
          sampling: 'average',
          large: true
        },
        {
          name: '声音传感器S4',
          type: 'line',
          data: soundData.s4,
          symbol: 'none',
          lineStyle: {
            width: 1.5,
            color: '#9932cc'
          },
          emphasis: {
            focus: 'none'
          },
          sampling: 'average',
          large: true
        }
      ],
      dataZoom: ChartConfig.getDataZoomConfig(20)
    };

    this.chartInstance.setOption(option, {
      notMerge: true,
      lazyUpdate: true,
      silent: true
    });
  },

  // 获取图表实例
  getInstance: function() {
    return this.chartInstance;
  },

  // 调整图表大小
  resize: function() {
    if (this.chartInstance) {
      this.chartInstance.resize();
    }
  },

  // 清空图表
  clear: function() {
    if (this.chartInstance) {
      this.chartInstance.clear();
    }
  },

  // 销毁图表
  dispose: function() {
    if (this.chartInstance) {
      ChartSync.unregisterChart(this.chartInstance);
      this.chartInstance.dispose();
      this.chartInstance = null;
    }
  }
};

// 导出到全局
window.SoundChart = SoundChart; 